//
//  BLMineViewController.m
//  blanket
//
//  Created by Mac on 2017/12/4.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLMineViewController.h"
#import "BLMineHeaderView.h"
#import "BLMineTableViewCell.h"
#import "BLSettingViewController.h"
#import "STAddressMangeController.h"
#import "BLToBeWashingViewController.h"
#import "BLCustomViewController.h"
#import "BLCommentViewController.h"
#import "BLOnsaleViewController.h"
#import "BLShopCarViewController.h"
#import "BLMineOrderViewController.h"
@interface BLMineViewController ()<BLMineHeaderViewDelegate>
@property(nonatomic, strong) BLMineHeaderView                     *header;
@property(nonatomic, strong) NSArray  <STTableEasyModel*>                   *dataSouce;
@property(nonatomic, strong) BLUserModel                     *userModel;
@end

@implementation BLMineViewController


#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的";
    [self configSubView];
    [self reloadData];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self sendMyInfoRequest];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    
    self.header = [[BLMineHeaderView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 10)];
    self.header.delegate = self;
    self.tableView.tableHeaderView = self.header;
}
- (void)reloadData{
    
    NSArray * sectionOne = @[
                             [[STTableEasyModel alloc] initWithTextString:@"申请洗涤业务" imageName:@"洗涤图标-2"],
                             [[STTableEasyModel alloc] initWithTextString:@"申请定制业务" imageName:@"定制"],
                            // [[STTableEasyModel alloc] initWithTextString:@"优惠券" imageName:@"优惠券(3)"],
                             [[STTableEasyModel alloc] initWithTextString:@"服务评价" imageName:@"服务评价"],
                             [[STTableEasyModel alloc] initWithTextString:@"购物车" imageName:@"购物车2"],
                             [[STTableEasyModel alloc] initWithTextString:@"收货地址" imageName:@"地址(3)"],
                             [[STTableEasyModel alloc] initWithTextString:@"发票邮寄地址" imageName:@"发票"]];
    
    
    self.dataSouce = sectionOne;
    self.header.userModel = self.userModel;
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    BLMineTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[BLMineTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.ghImageView.image = model.image;
    cell.lable.text = model.textString;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTableEasyModel * model = self.dataSouce[indexPath.row];

    if ([model.textString isEqualToString:@"申请洗涤业务"] ) {
        [self.navigationController pushViewController:[BLToBeWashingViewController new] animated:YES];
    }
    if ([model.textString isEqualToString:@"申请定制业务"] ) {
        [self.navigationController pushViewController:[BLCustomViewController new] animated:YES];
    }
    if ([model.textString isEqualToString:@"优惠券"] ) {
        [self.navigationController pushViewController:[BLOnsaleViewController new] animated:YES];
    }
    if ([model.textString isEqualToString:@"服务评价"] ) {
        if (!self.userModel) {
            [self sendMyInfoRequest];
            [SVProgressHUD showInfoWithStatus:@"请稍后"];
            return;
        }
        if (self.userModel.card.integerValue == 3) {
            //普通会员，没有资格评价
            [SVProgressHUD showInfoWithStatus:@"您是普通会员，暂不能进行服务评价哦"];
            return;
        }
        [self.navigationController pushViewController:[BLCommentViewController new] animated:YES];
    }
    if ([model.textString isEqualToString:@"购物车"] ) {
        [self.navigationController pushViewController:[BLShopCarViewController new] animated:YES];
    }
    if ([model.textString isEqualToString:@"收货地址"] ) {
        STAddressMangeController  * vc = [STAddressMangeController new];
        vc.type = @"1";
        [self.navigationController pushViewController:vc animated:YES];
    }
    if ([model.textString isEqualToString:@"发票邮寄地址"] ) {
        STAddressMangeController  * vc = [STAddressMangeController new];
        vc.type = @"2";
        [self.navigationController pushViewController:vc animated:YES];
    }

    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --BLMineHeaderViewDelegate
- (void)onSelectedIconButton{
    
}
- (void)onSelectedSettingButton{
    BLSettingViewController * vc =  [BLSettingViewController new];
    vc.userModel = self.userModel;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)onSelectedAllButton{
     [self.navigationController pushViewController:[BLMineOrderViewController new] animated:YES];
}

- (void)onSlectedOrderButton:(NSString*)title{
    [self.navigationController pushViewController:[BLMineOrderViewController new] animated:YES];
}
#pragma mark --NetWork Method
- (void)sendMyInfoRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/MemberListAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 self.userModel = [BLUserModel mj_objectWithKeyValues:responseObject[@"data"]];
                                                 [self reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end

